package com.cn.wanxi.front;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.NewsService;
import com.cn.wanxi.service.impl.NewsServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/front/findByNewsId")
public class NewsDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id = req.getParameter("id");
        System.out.println(id);
//        JSONObject jsonObject=new JSONObject();

        NewsService newsService = new NewsServiceImpl();
        ResultModel resultModel = null;
        if ("".equals(id)) {
            resultModel = ResultModel.getResultModel("error", -1, -1, null);
        } else {
            NewsModel newsModel = new NewsModel();
            newsModel.setId(Tool.stringToInt(id));
            resultModel = newsService.findByIdForFront(newsModel);
        }

        resp.getWriter().print(JSONObject.toJSONString(resultModel));
    }
}
